package com.xb.controller;

import com.xb.entity.Result;
import com.xb.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("home")
public class HomeController {

    @Autowired
    private HomeService service;

    @RequestMapping
    public Result index(){

        // 查询今日数据
        Map<String, Object> countData = service.findHomeCount();

        // 查询本周图表数据
        List<Map<String, Object>> detailData = service.findHomeDetail();

        // 转换为集合方式便于前端取出
        List<List> countList = new ArrayList<>();

        for (Map<String, Object> map : detailData) {

            List temp = new ArrayList();

            for (Map.Entry<String, Object> entry : map.entrySet()) {
                Object val = map.get(entry.getKey());
                temp.add(val);
            }
            countList.add(temp);
        }

        Map<String, Object> returnMap = new HashMap<>();

        // count数据
        returnMap.put("countData", countData);

        // 报表详细数据
        returnMap.put("detail", countList);

        return new Result(true, "查询成功", returnMap);
    }
}
